<html>
<head><meta charset="utf-8"><title>tuple layout guarantees · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/tuple.20layout.20guarantees.html">tuple layout guarantees</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="167782755"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/tuple%20layout%20guarantees/near/167782755" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/tuple.20layout.20guarantees.html#167782755">(Jun 10 2019 at 18:20)</a>:</h4>
<p>The reference sez:</p>
<blockquote>
<p>Tuples do not have any guarantees about their layout.</p>
</blockquote>
<p>Is this still accurate? Do we not make any guarantees about the layout (except for the empty tuple)?</p>



<a name="167783022"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/tuple%20layout%20guarantees/near/167783022" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/tuple.20layout.20guarantees.html#167783022">(Jun 10 2019 at 18:23)</a>:</h4>
<p>not having guarantees does not mean we dont want to, it just means nobody pushed through an RFC ;)</p>



<a name="167783053"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/tuple%20layout%20guarantees/near/167783053" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/tuple.20layout.20guarantees.html#167783053">(Jun 10 2019 at 18:23)</a>:</h4>
<p>also see <a href="https://github.com/rust-lang/unsafe-code-guidelines/blob/master/reference/src/layout/structs-and-tuples.md" target="_blank" title="https://github.com/rust-lang/unsafe-code-guidelines/blob/master/reference/src/layout/structs-and-tuples.md">https://github.com/rust-lang/unsafe-code-guidelines/blob/master/reference/src/layout/structs-and-tuples.md</a></p>



<a name="167783153"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/tuple%20layout%20guarantees/near/167783153" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/tuple.20layout.20guarantees.html#167783153">(Jun 10 2019 at 18:24)</a>:</h4>
<p>but generally, given that tuples are <code>repr(Rust)</code>, they are unlikely to get a lot of guarnatees</p>



<a name="167783235"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/tuple%20layout%20guarantees/near/167783235" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/tuple.20layout.20guarantees.html#167783235">(Jun 10 2019 at 18:25)</a>:</h4>
<p>Oh sure. And the current layout might become the guaranteed layout at some point as well, so code today might just work by luck.</p>



<a name="167783244"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/tuple%20layout%20guarantees/near/167783244" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/tuple.20layout.20guarantees.html#167783244">(Jun 10 2019 at 18:25)</a>:</h4>
<p>Context: <a href="https://stackoverflow.com/a/56529158/155423" target="_blank" title="https://stackoverflow.com/a/56529158/155423">How to convert a tuple of references to a reference of a tuple?</a></p>



<a name="167783406"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/tuple%20layout%20guarantees/near/167783406" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/tuple.20layout.20guarantees.html#167783406">(Jun 10 2019 at 18:27)</a>:</h4>
<p>we certainly don't guarantee that fields are laid out in order</p>



<a name="167783420"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/tuple%20layout%20guarantees/near/167783420" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/tuple.20layout.20guarantees.html#167783420">(Jun 10 2019 at 18:27)</a>:</h4>
<p>AFAIK tuples are subject to field reordering <em>except</em> for the last field (due to unsizing)</p>



<a name="167783464"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/tuple%20layout%20guarantees/near/167783464" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/tuple.20layout.20guarantees.html#167783464">(Jun 10 2019 at 18:28)</a>:</h4>
<p>Looks like it touches on "Homogeneous structs" and "(T1..Tn) would be compatible with TupleN&lt;T1..Tn&gt;"</p>



<a name="167783497"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/tuple%20layout%20guarantees/near/167783497" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/tuple.20layout.20guarantees.html#167783497">(Jun 10 2019 at 18:28)</a>:</h4>
<p><em>homogeneous</em> tuples are a special topic and it seems reasonable to say more there</p>



<a name="167783587"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/tuple%20layout%20guarantees/near/167783587" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/tuple.20layout.20guarantees.html#167783587">(Jun 10 2019 at 18:29)</a>:</h4>
<blockquote>
<p>it's probably invalid to rely on this</p>
</blockquote>
<p>not just probably, it <em>is</em> invalid :)</p>



<a name="167783589"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/tuple%20layout%20guarantees/near/167783589" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/tuple.20layout.20guarantees.html#167783589">(Jun 10 2019 at 18:29)</a>:</h4>
<p>The general question that OP asked is an easy answer: no, you can't go from <code>(&amp;A, &amp;B)</code> to <code>&amp;(A, B)</code> (right?)</p>



<a name="167783723"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/tuple%20layout%20guarantees/near/167783723" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/tuple.20layout.20guarantees.html#167783723">(Jun 10 2019 at 18:31)</a>:</h4>
<p>no you cannot, how would you even know they point to the same thing?</p>



<a name="167783732"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/tuple%20layout%20guarantees/near/167783732" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/tuple.20layout.20guarantees.html#167783732">(Jun 10 2019 at 18:31)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> nuances of the meaning of "invalid". The code compiles, produces the output you'd expect, and Miri doesn't complain, so it's "valid" in some sense. It's not valid in that it's UB</p>



<a name="167783745"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/tuple%20layout%20guarantees/near/167783745" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/tuple.20layout.20guarantees.html#167783745">(Jun 10 2019 at 18:31)</a>:</h4>
<p>and if you do know that they point to the same thing, in which situation would you not also know the base address of the thing?</p>



<a name="167783752"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/tuple%20layout%20guarantees/near/167783752" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/tuple.20layout.20guarantees.html#167783752">(Jun 10 2019 at 18:31)</a>:</h4>
<p>seems like XY problem to me TBH^^</p>



<a name="167783836"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/tuple%20layout%20guarantees/near/167783836" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/tuple.20layout.20guarantees.html#167783836">(Jun 10 2019 at 18:32)</a>:</h4>
<blockquote>
<p><span class="user-mention silent" data-user-id="120791">RalfJ</span> nuances of the meaning of "invalid". The code compiles, produces the output you'd expect, and Miri doesn't complain, so it's "valid" in some sense. It's not valid in that it's UB</p>
</blockquote>
<p>to be precise: it relies on explicitly unspecified parts of the semantics</p>



<a name="167783873"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/tuple%20layout%20guarantees/near/167783873" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/tuple.20layout.20guarantees.html#167783873">(Jun 10 2019 at 18:32)</a>:</h4>
<p>explicitly specified as unspecified ;-)</p>



<a name="167783886"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/tuple%20layout%20guarantees/near/167783886" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/tuple.20layout.20guarantees.html#167783886">(Jun 10 2019 at 18:32)</a>:</h4>
<blockquote>
<p>This page deliberately left blank</p>
</blockquote>



<a name="167783896"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/tuple%20layout%20guarantees/near/167783896" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/tuple.20layout.20guarantees.html#167783896">(Jun 10 2019 at 18:33)</a>:</h4>
<p>XY problem: absolutely</p>



<a name="167783915"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/tuple%20layout%20guarantees/near/167783915" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/tuple.20layout.20guarantees.html#167783915">(Jun 10 2019 at 18:33)</a>:</h4>
<p>that is invalid by any measure I know of ;) it "happens to work" is the best you can say about it</p>



<a name="167784207"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/tuple%20layout%20guarantees/near/167784207" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/tuple.20layout.20guarantees.html#167784207">(Jun 10 2019 at 18:36)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> this is where you realize the huge mistake you've participated in. Miri reports no issues with the code, that means that there's no UB. This is how it works.</p>



<a name="167784248"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/tuple%20layout%20guarantees/near/167784248" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/tuple.20layout.20guarantees.html#167784248">(Jun 10 2019 at 18:36)</a>:</h4>
<p>I understand that's not true, but I do think that's a likely thing for people to think.</p>



<a name="167784269"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/tuple%20layout%20guarantees/near/167784269" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/tuple.20layout.20guarantees.html#167784269">(Jun 10 2019 at 18:36)</a>:</h4>
<p>And I think I've started from that assumption once or twice.</p>



<a name="167790197"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/tuple%20layout%20guarantees/near/167790197" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/tuple.20layout.20guarantees.html#167790197">(Jun 10 2019 at 19:47)</a>:</h4>
<blockquote>
<p><span class="user-mention silent" data-user-id="120791">RalfJ</span> this is where you realize the huge mistake you've participated in. Miri reports no issues with the code, that means that there's no UB. This is how it works.</p>
</blockquote>
<p>and indeed there is no UB -- with this version of Rust on this platform</p>



<a name="167795202"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/tuple%20layout%20guarantees/near/167795202" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/tuple.20layout.20guarantees.html#167795202">(Jun 10 2019 at 20:49)</a>:</h4>
<p>isn't that kind of a half-truth? The behavior is undefined, by definition even.</p>



<a name="167797313"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/tuple%20layout%20guarantees/near/167797313" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/tuple.20layout.20guarantees.html#167797313">(Jun 10 2019 at 21:16)</a>:</h4>
<p>well it's a matter of definition mostly. the way I see it, the Rust Abstract Machine is parameterized by how data structures are laid out. Miri tests for UB <em>with a particular instance</em> of that parameter, and there is no UB there.</p>



<a name="167797390"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/tuple%20layout%20guarantees/near/167797390" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/tuple.20layout.20guarantees.html#167797390">(Jun 10 2019 at 21:17)</a>:</h4>
<p>the property we probably care about for programs is that they are UB-free under <em>any instance</em>. And now if we are a bit sloppy we might call that property "being UB-free" as well.</p>



<a name="167797510"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/tuple%20layout%20guarantees/near/167797510" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/tuple.20layout.20guarantees.html#167797510">(Jun 10 2019 at 21:19)</a>:</h4>
<p>but you can arrange the words differently here of course ;)<br>
writing the spec in a way that such a program has UB under any execution without having to resort to this "hidden parameter" is very non-trivial. that's akin to changing Miri such that it can detect such issues <em>deterministically</em>. so I'd argue that this is a more elegant way to set up the semantics.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>